var Variance = 3;
// Motor Brands
MotorBrands = new Array("Novak", "Orion", "Hacker Brushless", "Xcelorin 10th Scale", "Castle Creations", "Misc Brushed", "Fantom");

// Car Types
CarTypes = new Array("4WD Touring Car", "2WD Buggy", "2WD Off-Road Truck", "CORR Truck", "4WD Buggy");

//    Format for Cars array: (
//    0 > CarID
//    1 > Make 
//    2 > Model
//    3 > TransRatio 
//    4 > CarType
//    5 > Spur1 Gear sizes to use

CarsRollOut = new Array("1/12 On-Road", "1/10 On-Road");

Cars = new Array(
    Array("Custom", "", "2.6", 0, Array("72", "75", "78", "84")),
    Array("Associated", "RC10B4", "2.6", 1, Array("72", "75", "78", "84")),
    Array("Associated", "RC10T4", "2.6", 2, Array("72", "75", "78", "84")),
    Array("Associated", "B44", "2.5", 4, Array("72", "75", "78", "84")),
    Array("Associated", "RC10B3", "2.4", 1, Array("72", "75", "78", "84")),
    Array("Associated", "SC10", "2.6", 3, Array("72", "75", "78", "87")),
    Array("Associated", "TC3", "2.5", 0, Array("88", "94", "100", "106")),
    Array("Associated", "TC4", "2.5", 0, Array("72", "75", "78", "84")),
    Array("Associated", "TC5", "2.0", 0, Array("72", "75", "78", "84")),
    Array("Hot Bodies", "Cyclone TC", "2.16", 0, Array("72", "75", "78", "84")),
    Array("Kyosho", "Lazer ZX-5 SP", "2.5", 4, Array("72", "75", "78", "84")),
    Array("Kyosho", "Lazer ZX-5 FS", "2.5", 4, Array("72", "75", "78", "84")),
    Array("Kyosho", "Ultima RB5", "2.6", 1, Array("72", "75", "78", "84")),
    Array("Losi", "2WD Buggy", "2.43", 1, Array("72", "75", "78", "84")),
    Array("Losi", "2WD Truck", "2.43", 2, Array("72", "75", "78", "84")),
    Array("Losi", "XXX-4", "2.1", 4, Array("72", "75", "78", "84")),
    Array("Schumacher", "Cat SX - 2.5", "2.5", 4, Array("72", "75", "78", "84")),
    Array("Schumacher", "Cat SX - 2.6", "2.6", 4, Array("72", "75", "78", "84")),
    Array("Schumacher", "Cat SX - 2.7", "2.7", 4, Array("72", "75", "78", "84")),
    Array("Losi", "XXX-S", "1.8262", 0, Array("72", "75", "78", "84")),
    Array("Tamiya", "Evolution 3", "2.6", 0, Array("72", "75", "78", "84")),
    Array("Tamiya", "TA04R. 414M", "2.13", 1, Array("72", "75", "78", "84")),
    Array("Traxxas", "Rustler", "2.72", 2, Array("72", "83", "86", "90")),
    Array("Traxxas", "Slash", "2.72", 3, Array("72", "83", "86", "90")),
    Array("X Ray", "Xray", "2.125", 0, Array("72", "75", "78", "84"))
);

// All Motor Types
// (Motor Type, Touring Car, Buggy, Truck, CORR, 4WD OffRoad)
// Novak, Orion, Hacker, XCelorin, Castle, brushed
AllMotorTypes = new 
Array(
    Array(
        Array(3.5, 10.5, 13, 14, -1, 13),
        Array(4.5, 9.5, 12, 13, -1, 12),
        Array(5.5, 8.5, 11, 12.5, -1, 11),
        Array(6.5, 7.5, 10, 11.5, -1, 10),
        Array(7.5, 7, 9.5, 11, -1, 9.5),
        Array(8.5, 6.5, 9, 10.5, -1, 9),
        Array(10.5, 5.5, 8, 9.5, 9.5, 8),
        Array(13.5, 4.5, 7, 8.5, 7.5, 7),
        Array(17.5, 3.5, 6, 7.5, 7, 6),
        Array(21.5, 4.2, 5, 6.5, -1, 5)
    ),
    Array(
        Array(2.5, 11.5, 13.5, 14, -1, 14),
        Array(3.0, 11, 13, 14.5, -1, 13.5),
        Array(3.5, 10.5, 12.5, 14, -1, 13),
        Array(4.0, 10, 12, 13.5, -1, 12.5),
        Array(4.5, 9.5, 11.5, 13, -1, 12),
        Array(5.5, 8.5, 11, 12.5, -1, 11),
        Array(6.5, 7.5, 10, 11.5, -1, 10),
        Array(7.5, 7, 9.5, 11, -1, 9.5),
        Array(9.5, 6.5, 9, 10.5, -1, 9),
        Array(10.5, 5.5, 8, 9.5, -1, 8),
        Array(13.5, 4.5, 7, 8.5, -1, 7),
        Array(17.5, 3.5, 6, 7.5, -1, 6),
        Array("7500 SL", 10, 12, 13.5, -1, 12.5),
        Array("6400 SL", 8.5, 11, 12.5, -1, 11)
    ),
    Array(
        Array("E40 3.5", 10.2, -1, -1, -1, -1),
        Array("E40 4.5", 9.5, -1, -1, -1, -1),
        Array("E40 5.5", -1, -1, -1, -1, 9.45),
        Array("E40 6.5", -1, -1, 8.67, -1, -1),
        Array("E40 7.5", -1, 8.7, -1, -1, -1),
        Array("E40 13.5", 4.5, -1, -1, -1, -1),
        Array("E40 17.5", 4.5, -1, -1, -1, -1)
    ),
    Array(
        Array("3.5", 9.8, 13.4, 14.1, -1, 14.8),
        Array("4.5", 8.4, 12.5, 13.2, -1, 13.7),
        Array("6.5", 7.3, 10.5, 11.7, -1, 10.4),
        Array("8.5", 7, 9.6, 10.7, -1, 9.3),
        Array("10.5", 5.4, 8.3, 9.8, -1, 8.2),
        Array("13.5", 4.6, 7.2, 8.8, -1, 7.4),
        Array("17.5", 3.8, 6, 7.6, -1, 6.3)
    ),
    Array(
        Array("5700kv", -1, -1, -1, -1, 13.125)
    ),
    Array(
        Array("19 Turn", -1, 8, 11.91, -1, -1),
        Array("24 deg Stock", -1, -1, 11.3, -1, -1, -1),
        Array("14t Mod", -1, -1, 10.28, -1, -1, -1),
        Array("13t Mod", -1, -1, 10.77, -1, -1, -1),
        Array("12t Mod", -1, -1, 11.31, -1, -1, -1),
        Array("11t Mod", -1, -1, 11.91, -1, -1, -1),
        Array("10t Mod", -1, -1, 12.57, -1, -1, -1)
    ),
    Array(
        Array("17.5", -1, -1, 6.6, -1, -1)
    )
);
// (Motor Type, Touring Car, 2wd Buggy, Truck, CORR, 4WD OffRoad)


// Motor Types
// (Motor Type, Touring Car, Buggy, Truck, CORR)
MotorsNovak = new Array(
        Array(3.5, 10.5, 13, 14),
        Array(4.5, 9.5, 12, 13),
        Array(5.5, 8.5, 11, 12.5),
        Array(6.5, 7.5, 10, 11.5),
        Array(7.5, 7, 9.5, 11),
        Array(8.5, 6.5, 9, 10.5),
        Array(10.5, 5.5, 8, 9.5, 9.5),
        Array(13.5, 4.5, 7, 8.5, 7.5),
        Array(17.5, 3.5, 6, 7.5, 7),
        Array(21.5, 4.2, 5, 6.5)
);

function increase(i) {
    // Increase the pinion gear size
    // then manually recalculate
    //var evDivSpur = eval("divSpur" + i);
    //var evDivFDR = eval("divFDR" + i);
    //var evDivPinion = eval("divPinion" + i);

    var Spur = 0;
    var Pinion = 0;
    var TransRatio = document.getElementById("TransRatio").value;
    switch (i) {
        case 1:
            Spur = document.getElementById("divSpur1").innerHTML;
            document.getElementById("divPinion1").innerHTML++;
            Pinion = document.getElementById("divPinion1").innerHTML;
            document.getElementById("divFDR1").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR1").innerHTML);
            break;
        case 2:
            Spur = document.getElementById("divSpur2").innerHTML;
            document.getElementById("divPinion2").innerHTML++;
            Pinion = document.getElementById("divPinion2").innerHTML;
            document.getElementById("divFDR2").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR2").innerHTML);
            break;
        case 3:
            Spur = document.getElementById("divSpur3").innerHTML;
            document.getElementById("divPinion3").innerHTML++;
            Pinion = document.getElementById("divPinion3").innerHTML;
            document.getElementById("divFDR3").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR3").innerHTML);
            break;
        case 4:
            Spur = document.getElementById("divSpur4").innerHTML;
            document.getElementById("divPinion4").innerHTML++;
            Pinion = document.getElementById("divPinion4").innerHTML;
            document.getElementById("divFDR4").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR4").innerHTML);
            break;
    }

    //var Spur = evDivSpur.innerHTML;
    //         evDivPinion.innerHTML++;
    //         var Pinion = evDivPinion.innerHTML;
    //         evDivFDR.innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
    // Set the notes
    //         SetNotes(i, evDivFDR.innerHTML);
}

function decrease(i) {
    var Spur = 0;
    var Pinion = 0;
    var TransRatio = document.getElementById("TransRatio").value;
    switch (i) {
        case 1:
            Spur = document.getElementById("divSpur1").innerHTML;
            document.getElementById("divPinion1").innerHTML--;
            Pinion = document.getElementById("divPinion1").innerHTML;
            document.getElementById("divFDR1").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR1").innerHTML);
            break;
        case 2:
            Spur = document.getElementById("divSpur2").innerHTML;
            document.getElementById("divPinion2").innerHTML--;
            Pinion = document.getElementById("divPinion2").innerHTML;
            document.getElementById("divFDR2").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR2").innerHTML);
            break;
        case 3:
            Spur = document.getElementById("divSpur3").innerHTML;
            document.getElementById("divPinion3").innerHTML--;
            Pinion = document.getElementById("divPinion3").innerHTML;
            document.getElementById("divFDR3").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR3").innerHTML);
            break;
        case 4:
            Spur = document.getElementById("divSpur4").innerHTML;
            document.getElementById("divPinion4").innerHTML--;
            Pinion = document.getElementById("divPinion4").innerHTML;
            document.getElementById("divFDR4").innerHTML = Math.round(((Spur / Pinion) * TransRatio) * 100) / 100;
            SetNotes(i, document.getElementById("divFDR4").innerHTML);
            break;
    }
}

function PopulateCarDropDown() {
    for (var i = 0; i < Cars.length; i++) {
        document.getElementById("CarID").options[i] = new Option(Cars[i][0] + " " + Cars[i][1], i);
    }
}


function PopulateMotorBrandsDropDown() {
    for (var i = 0; i < MotorBrands.length; i++) {
        document.getElementById("MotorBrandID").options[i] = new Option(MotorBrands[i], i);

    }
}
function PopulateCarTypeDropDown() {
    // The one for finding ratios
    for (var i = 0; i < CarTypes.length; i++) {
        document.getElementById("CarType").options[i] = new Option(CarTypes[i], i);
//        document.getElementById("NewVehicleType").options[i] = new Option(CarTypes[i], CarTypes[i]);
//        document.getElementById("SuggestedVehicleTypeID").options[i] = new Option(CarTypes[i], CarTypes[i]);

    }
}

function PopulateMotorTypeDropDown(type, WhatToDo) {
    // WhatToDo = 1 - Page load/refresh
    // WhatToDo = 2 - Changed Motor Brand

    var MotorBrandID = 0;
    // For some reason, the MotorBrandID selected index value is set to -1 as default
    // So let's just change it to 0 for the first motor brand (Novak)
    //    if (document.getElementById("MotorBrandID").selectedIndex == "-1") {
    //    } else {
    //    }

    var ExistingMotorTypeID = 0;

    switch (WhatToDo) {
        case 1:
            MotorBrandID = 0;
            //            ExistingMotorTypeID = "10.5";
            ExistingMotorTypeID = AllMotorTypes[0][0][0];
            break;
        case 2:
            // Check the existing value so we can use it if it's in the new list
            MotorBrandID = document.getElementById("MotorBrandID").selectedIndex;
            ExistingMotorTypeID = document.getElementById("MotorID").value;
            break;
    }
    //alert("ExistingMotorTypeID: " + ExistingMotorTypeID);
    // Erease previous entries
    for (var i = (document.getElementById("MotorID").options.length - 1); i >= 0; i--) {
        document.getElementById("MotorID").options[i] = null;
    }
    // Now populate with new values
    for (var i = 0; i < AllMotorTypes[MotorBrandID].length; i++) {
        document.getElementById("MotorID").options[i] = new Option(AllMotorTypes[MotorBrandID][i][0], AllMotorTypes[MotorBrandID][i][0]);
        if (ExistingMotorTypeID == AllMotorTypes[MotorBrandID][i][0]) {
            document.getElementById("MotorID").value = ExistingMotorTypeID;
        }
    }

    // Set the motor type to previous setting, if we can
    //    var selectobject = document.getElementById("MotorID")
    //    selectobject.value = ExistkngMotorTypeID;
    //    for (var i = 0; i < selectobject.length; i++) {
    //        alert(selectobject.options[i].text + " " + selectobject.options[i].value);
    //    }

    if (WhatToDo == 2) GetRatios(type);
}

function PopulateSpurGears(type) {
    var CurrentCarID = document.getElementById("CarID").value;
    //alert(Cars[CurrentCarID][4][0]);
    document.getElementById("Spur1").value = Cars[CurrentCarID][4][0];
    if (type == "standard") {
        document.getElementById("Spur2").value = Cars[CurrentCarID][4][1];
        document.getElementById("Spur3").value = Cars[CurrentCarID][4][2];
        document.getElementById("Spur4").value = Cars[CurrentCarID][4][3];
    }
}

function CreateResultsTable(rowCount, colCount, srcHolder) {
    srcHolder.innerHTML = "";
    var srcTable = document.createElement("table");
    srcTable.border = 1;
    srcTable.height = 100;
    srcTable.width = 100;

    var tmpRow = null;
    var tmpCell = null;

    // Adds an empty table within the div tags
    srcHolder.appendChild(srcTable);

    // Populates the table
    for (i = 0; i < rowCount; i++) {
        // Add a row to the table
        tmpRow = AppendRow(srcTable);
        for (j = 0; j < colCount; j++) {
            tmpCell = AppendCell(tmpRow);
            tmpCell.innerHTML = j;
            tmpCell = null;
        }
    }

}


function isValidNumber(ipNum) {
    if (isNaN(ipNum)) {
        alert("Invlaid Number");
        return false;
    } else if (ipNum < 1) {
        alert("Number should be greater than 0");
        return false;
    } else {
        return true;
    }
}

function PopulateAllControls(type) {
    PopulateCarDropDown(type);
    PopulateCarTypeDropDown(type);
    PopulateSpurGears(type);
    PopulateMotorBrandsDropDown(type);
    PopulateMotorTypeDropDown(type, 1);
    GetRatios(type);
}

function GetRatios(type) {
    // Set all the variables
    // Get the Car Type
    // For Pre-Determined cars
    var CarID = document.getElementById("CarID").value;
    var MotorBrandID = document.getElementById("MotorBrandID").selectedIndex;
    if (MotorBrandID == -1) MotorBrandID = 0;
    if (CarID > 0) {
        // This is not a custom ratio
        var CarType = Cars[CarID][3];
        CarType++;
        // Get the trans ratio for this car
        var TransRatio = Cars[CarID][2];
        document.getElementById("TransRatio").disabled = true;
        document.getElementById("CarType").disabled = true;
        document.getElementById("TransRatio").value = TransRatio;
        document.getElementById("CarType").selectedIndex = CarType - 1;
    } else {
        // This is a custom ratio
        var CarType = document.getElementById("CarType").selectedIndex;
        CarType++;
        var TransRatio = document.getElementById("TransRatio").value;
        SpurGears = new Array(72, 75, 78, 84);
        document.getElementById("TransRatio").disabled = false;
        document.getElementById("CarType").disabled = false;
    }
    var IdealRatio = AllMotorTypes[MotorBrandID][document.getElementById("MotorID").selectedIndex][CarType];
    document.getElementById("IdealRatio").innerHTML = IdealRatio;

    SpurGears = new Array(document.getElementById("Spur1").value);
    if (type == "standard") {
        SpurGears.push(document.getElementById("Spur2").value);
        SpurGears.push(document.getElementById("Spur3").value);
        SpurGears.push(document.getElementById("Spur4").value);
    }

    SpurGears.sort();
    // We know what the ideal ratio, TransRatio, and spur gears are now
    // we need to figure out the pinion gear sizes using this equation:
    // Pinion = (Spur / (IdealRatio / TransRatio)

    // Cycle through the spur gear sizes
    var Spur = null;
    var i2 = 1;
    for (i = 0; i < 4; i++) {
        Spur = SpurGears[i];
        var Pinion = Math.round((Spur / (IdealRatio / TransRatio)));

        switch (i2) {
            case 1:
                document.getElementById("divPinion1").innerHTML = Pinion;
                FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
                document.getElementById("divSpur1").innerHTML = Spur;
                document.getElementById("divFDR1").innerHTML = FDR;
                SetNotes(1, FDR, type);
                // 			       	alert("Ideal Ratio:2a " + IdealRatio);
                break;
            case 2:
                if (type == "mobile") break;
                document.getElementById("divPinion2").innerHTML = Pinion;
                FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
                document.getElementById("divSpur2").innerHTML = Spur;
                document.getElementById("divFDR2").innerHTML = FDR;
                SetNotes(2, FDR, type);
                // 			       	alert("Ideal Ratio:2b " + IdealRatio);
                break;
            case 3:
                if (type == "mobile") break;
                document.getElementById("divPinion3").innerHTML = Pinion;
                FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
                document.getElementById("divSpur3").innerHTML = Spur;
                document.getElementById("divFDR3").innerHTML = FDR;
                SetNotes(3, FDR, type);
                // 			       	alert("Ideal Ratio:2c " + IdealRatio);
                break;
            case 4:
                if (type == "mobile") break;
                document.getElementById("divPinion4").innerHTML = Pinion;
                FDR = Math.round((Spur / Pinion * TransRatio) * 100) / 100;
                document.getElementById("divSpur4").innerHTML = Spur;
                document.getElementById("divFDR4").innerHTML = FDR;
                SetNotes(4, FDR, type);
                // 			       	alert("Ideal Ratio:2d " + IdealRatio);
                break;
        }

        document.getElementById("IdealRatio").innerHTML = IdealRatio;

        i2++;
    }
}

function SetNotes(i, FDR, type) {
    // Figure out the notes
    // Variances:
    //      0       Bang-on
    //      < 1.5%  Good
    //      < 3%    Slightly
    //      > 3%    Greatly
    var IdealRatio = document.getElementById("IdealRatio").innerHTML;
    var Variance = (IdealRatio - FDR) / IdealRatio * 100;

    //alert("Ratio " + i + ", Variance: " + Variance + ", FDR: " + FDR + ", IdealRatio: " + IdealRatio);

    Variance = Math.round(Variance * 100) / 100;
    Notes = "";
    NotesclassName = "";
    if (Variance < -4) {
        Notes = "Excessively Under-Geared: " + Math.abs(Variance) + "% off";
        NotesclassName = "Excessively";
    } else
        if (Variance < -3) {
        Notes = "Moderatly Under-geared: " + Math.abs(Variance) + "% off";
        NotesclassName = "Moderately";
    } else
        if (Variance < 0) {
        Notes = "Slightly Under-geared: " + Variance + "% off";
        NotesclassName = "Slightly";
    } else
        if (Variance == 0) {
        Notes = "Dead-on-balls accurate!";
        NotesclassName = "Accurate";
    } else
        if (Variance < 3) {
        Notes = "Slightly Over-Geared: " + Variance + "% off";
        NotesclassName = "Slightly";
    } else
        if (Variance < 5) {
        Notes = "Moderately Over-Geared: " + Variance + "% off";
        NotesclassName = "Moderately";
    } else
        if (Variance >= 5) {
        Notes = "Excessively Over-Geared!: " + Variance + "% off";
        NotesclassName = "Excessively";
    }
    if (FDR == -1) {
        Notes = "No Data available for this combination";
        IdealRatio = "";
        document.getElementById("divPinion1").innerHTML = " ";
        document.getElementById("divPinion2").innerHTML = " ";
        document.getElementById("divPinion3").innerHTML = " ";
        document.getElementById("divPinion4").innerHTML = " ";

        document.getElementById("divFDR1").innerHTML = " &nbsp";
        document.getElementById("divFDR2").innerHTML = " &nbsp";
        document.getElementById("divFDR3").innerHTML = " &nbsp";
        document.getElementById("divFDR4").innerHTML = " &nbsp";
    }
    //     if (type == "standard") {
    switch (i) {
        case 1:
            document.getElementById("Notes1").innerHTML = Notes;
            document.getElementById("Notes1").className = NotesclassName;
            break;
        case 2:
            document.getElementById("Notes2").innerHTML = Notes;
            document.getElementById("Notes2").className = NotesclassName;
            break;
        case 3:
            document.getElementById("Notes3").innerHTML = Notes;
            document.getElementById("Notes3").className = NotesclassName;
            break;
        case 4:
            document.getElementById("Notes4").innerHTML = Notes;
            document.getElementById("Notes4").className = NotesclassName;
            break;
    }
    //     }
}

function CarChanged(type) {
    // Update Spur gear selections
    GetRatios(type);
}
function ChangeCurser() {
    document.body.style.cursor = 'pointer';
}
function ChangeCurserBack() {
    document.body.style.cursor = 'default';
}
function ViewChart() {
    //var evDivSpur = eval("divSpur" + i);
    //var evDivFDR = eval("divFDR" + i);
    //var evDivPinion = eval("divPinion" + i);

    var Spur1 = document.getElementById("divSpur1").innerHTML;
    var Spur2 = document.getElementById("divSpur2").innerHTML;
    var Spur3 = document.getElementById("divSpur3").innerHTML;
    var Spur4 = document.getElementById("divSpur4").innerHTML;

    //var Pinion = document.getElementById("ChartPinion").value = evDivPinion.innerHTML;
    var Pinion = document.getElementById("divPinion1").innerHTML;
    //document.getElementById("ChartSpur").value = evDivSpur.innerHTML;
    document.getElementById("ChartTransRatio").value = document.getElementById("TransRatio").value;
    var StartPinion = document.getElementById("divPinion1").innerHTML;
    var EndPinion = document.getElementById("divPinion4").innerHTML;

    var Make = document.getElementById("CarID").options[document.getElementById("CarID").value].text;
    var MotorBrand = document.getElementById("MotorBrandID").options[document.getElementById("MotorBrandID").value].text;
    var Motor = document.getElementById("MotorID").value;

    var Link = "gchart.php?StartPinion=" + StartPinion + "&EndPinion=" + EndPinion + "&Spur1=" + Spur1 + "&Spur2=" + Spur2 + "&Spur3=" + Spur3 + "&Spur4=" + Spur4 + "&IdealRatio=" + document.getElementById("IdealRatio").innerHTML + "&TransRatio=" + document.getElementById("TransRatio").value + "&Make=" + Make + "&Motor=" + Motor + "&MotorBrand=" + MotorBrand;
    ChartWindow = window.open(Link, "Chart");
}

function SpurGearUpdated(type) {
    // We want to reorg the spur gear list so that it's in order from
    GetRatios(type);
}

function PopulateChart(i) {
    if (i == null) {
        var StartPinion = document.getElementById("divPinion1").innerHTML;
        StartPinion = StartPinion - 3;
        var EndPinion = document.getElementById("divPinion4").innerHTML;
    } else {
        i++;
        var divPinion = eval("divPinion" + i);
        var Pinion = divPinion.innerHTML;
        var StartPinion = Pinion - 3;
        var EndPinion = Pinion;
    }
    EndPinion++;
    EndPinion++;
    EndPinion++;

    //alert("StartPinion: " + StartPinion + ", EndPinion: " + EndPinion);

    document.getElementById("tblSpur1").innerHTML = document.getElementById("Spur1").value;
    document.getElementById("tblSpur2").innerHTML = document.getElementById("Spur2").value;
    document.getElementById("tblSpur3").innerHTML = document.getElementById("Spur3").value;
    document.getElementById("tblSpur4").innerHTML = document.getElementById("Spur4").value;
}

function CalculateFDR(theOption) {
    var theResult;
    var theTransRatio = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedTransRatio").value;
    var thePinion = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedPinion").value;
    var theSpur = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedSpur").value;

    if (!isNaN(theTransRatio) && theTransRatio.length > 0 &&
	    !isNaN(thePinion) && thePinion.length > 0 &&
	    !isNaN(theSpur) && theSpur.length > 0) {
        //	    alert("yes");
        switch (theOption) {
            case "SubmitSuggested":
                var thePinion = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedPinion").value;
                var theSpur = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedSpur").value;
                var theTransRatio = document.getElementById("ctl00_ContentPlaceHolder1_SuggestedTransRatio").value;
                break;
        }
        theResult = Math.round((theSpur / thePinion * theTransRatio) * 100) / 100;
        document.getElementById("ctl00_ContentPlaceHolder1_SuggestedFDR").value = theResult;
    } else {
        //	    alert("No");
    document.getElementById("ctl00_ContentPlaceHolder1_SuggestedFDR").value = "";
    }
}